from PyQt4 import QtCore
from PyQt4.QtCore import QObject,QThread
from KDataBase.KDBProvider import KAbstractDBProvider
from KDataBase.KDBUtility import KDBUtility
import time
import os ;
import re ;
import datetime;
class KEODProvider(KAbstractDBProvider.KAbstractDBProvider):
	def __init__(self,parent,database):
		super(KEODProvider,self).__init__(parent,database)

	##EOD related functions
	def BuildAllHistory(self,path):
	## scan all market under current path,the hieracy of EOD data should be EOD--Market--Year--Date
		self.stopped=False   ## stop running flag   
		self.start=time.clock()
		subfolders = os.listdir(path)
		for sub_dir in subfolders:
			abspath=os.path.join(path, sub_dir)
			if os.path.isdir(abspath):
				finished=self.__BuildHistory(abspath,sub_dir) ## scan each market
				if finished==False: ## return because interrupted by user.
					return False
		self.UpdateProgress("Finished Scanning EOD History")
		return True
	def __BuildHistory(self,folder,market):
	## Build current market from EOD data provider
		subfolders = os.listdir(folder)
		##self.db.drop_collection(self.GetCollectionName("us",market,False))
		self.cldailystocks=self.db[KDBUtility.GetCollectionName("us",market,False)]
		datacount=0
		upcontents=[]
		if market !='NASDAQ':
			pass	
		for entry in subfolders:
			abspath=os.path.join(folder, entry)
			if os.path.isdir(abspath): #scan for each year
				subfolder= os.listdir(abspath)
				for file in subfolder:
					(temp,sdate)=file.split('_',2)
					(sdate,suffix)=sdate.split('.',2)
					data=open(os.path.join(abspath,file))
					next(data)#skip first line
					for each_line in data: #scan for each date
						if suffix=='sp':
							(tick,openp,highp,lowp,closep,vol,name)=each_line.split(',',7)
						else:
							(tick,temp,openp,highp,lowp,closep,vol)=each_line.split(',',7)						
						tick=tick.upper().replace('.','/')
						if tick.upper()!='AAPL':
							pass
						priceblock=[openp,highp,lowp,closep,vol]
						newcondition={"symbol":tick,"date":sdate}
						upcontents+=[{"symbol":tick,"date":sdate,"price":priceblock,"provider":'eod'}]
						datacount+=1
						if datacount%self.bulklimit==0:
							self.BulkInsertBySingleStockDate(market,datacount,upcontents)
							upcontents=[]
					data.close()
					if self.Stop:    
						return False 
		if upcontents:
			self.BulkInsertBySingleStockDate(market,datacount,upcontents) # plug in the rest
		self.cldailystocks.ensure_index([("symbol",1)]) ## ensure index
		self.UpdateProgress("Ensure Index for M:%s" %(market))
		return True